import { post, get, put, del } from '../utils/request';

/**
 * 店铺管理相关API
 */

// 商品管理
export const getCommodityList = (params?: {
  page?: number;
  pageSize?: number;
  status?: string;
  category?: string;
  keyword?: string;
}) => {
  return get('/v1/shop/commodity/list', params);
};

// 获取商品列表 - 新的接口
export const getProductList = (params: {
  page: number;
  page_size: number;
  keyword?: string;
  sale_status?: number;
}) => {
  return post('/v1/merchant/products/list', params);
};

// 商品状态变更
export const updateProductStatus = (productId: number, data: {
  sale_status: number;
  reason: string;
}) => {
  return put(`/v1/merchant/products/${productId}/status`, data);
};

// 创建商品
export const createProduct = (data: {
  product_name: string;
  product_code: string;
  category_id: number;
  stock_quantity: number;
  product_tags: string;
  sales_type: number;
  product_price: number;
  cost_price: number;
  support_life_points: number;
  description: string;
  proxy_commission_ratio: number;
  sales_price: number;
  attachments: string;
  skus: Array<{
    sku_name: string;
    sku_price: number;
    stock: number;
    strikethrough_price: number;
    spec_json: string;
    spec_type: string;
    cost_price: number;
    stock_warning_threshold: number;
  }>;
}) => {
  return post('/v1/merchant/products', data);
};

// 获取OSS签名
export const getOssSign = (data: { bucket: string }) => {
  return post('/v1/getOssSign', data);
};

// 删除商品
export const deleteProduct = (productId: number) => {
  return del(`/v1/merchant/products/${productId}`);
};

// 获取订单列表
export const getOrderList = (params: {
  page: number;
  page_size: number;
  keyword?: string;
  order_status?: number;
  pay_status?: number;
  ship_status?: number;
  sales_type?: number;
}) => {
  return post('/v1/merchant/orders/list', params);
};

// 取消订单
export const cancelOrder = (orderId: number, data: { cancel_reason: string }) => {
  return put(`/v1/merchant/orders/${orderId}/cancel`, data);
};

// 立即发货
export const shipOrder = (orderId: number, data: { delivery_remark: string }) => {
  return post(`/v1/merchant/orders/${orderId}/ship`, data);
};

// 更新订单状态
export const updateOrderStatus = (orderId: number, data: { 
  order_status: number; 
  status_remark: string; 
}) => {
  return put(`/v1/merchant/orders/${orderId}/status`, data);
};

// 更新快递单号
export const updateOrderTracking = (orderId: number, data: { 
  tracking_number: string; 
  express_company: string; 
}) => {
  return put(`/v1/merchant/orders/${orderId}/tracking`, data);
};

// 订单核销
export const verifyOrder = (orderId: number, data: { 
  verification_location: string; 
  verification_code: string; 
  verification_remark: string; 
}) => {
  return post(`/v1/merchant/orders/${orderId}/verify`, data);
};

// 批量录入快递单号
export const batchUpdateTracking = (data: { 
  tracking_data: string; 
}) => {
  return post('/v1/merchant/orders/batch-tracking', data);
};

// 获取代理配置列表
export const getProxyConfigList = () => {
  return post('/v1/merchant/proxies/config/list');
};

// 获取代理商列表
export const getProxyList = (params: {
  page: number;
  page_size: number;
  keyword?: string;
}) => {
  return post('/v1/merchant/proxies/list', params);
};

// 更新代理配置
export const updateProxyConfig = (data: {
  configs: Array<{
    level: number;
    amount: number;
    sales: number;
    commission_rate: number;
  }>;
}) => {
  return put('/v1/merchant/proxies/config', data);
};

// 更新商品
export const updateProduct = (productId: number, data: {
  product_name?: string;
  product_price?: number;
  description?: string;
  skus?: Array<{
    id?: number;
    sku_name?: string;
    sku_price?: number;
    stock?: number;
  }>;
}) => {
  return put(`/v1/merchant/products/${productId}`, data);
};

export const getCommodityDetail = (id: string) => {
  return get(`/v1/shop/commodity/${id}`);
};

export const addCommodity = (data: {
  name: string;
  price: number;
  category: string;
  description?: string;
  images?: string[];
  stock?: number;
}) => {
  return post('/v1/shop/commodity', data);
};

export const updateCommodity = (id: string, data: {
  name?: string;
  price?: number;
  category?: string;
  description?: string;
  images?: string[];
  stock?: number;
}) => {
  return put(`/v1/shop/commodity/${id}`, data);
};

export const deleteCommodity = (id: string) => {
  return del(`/v1/shop/commodity/${id}`);
};


export const getOrderDetail = (id: string) => {
  return get(`/v1/shop/order/${id}`);
};


// 数据经营
export const getBusinessData = (params?: {
  startDate?: string;
  endDate?: string;
  type?: string;
}) => {
  return get('/v1/shop/business/data', params);
};

export const getSalesStats = (params?: {
  startDate?: string;
  endDate?: string;
  period?: string;
}) => {
  return get('/v1/shop/sales/stats', params);
};
